package com.example.demo.controller;
import com.example.demo.entity.GitDown;
import com.example.demo.entity.ReturnT;
import com.example.demo.repository.GitDownRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @description git_down
 * @author zhengkai.blog.csdn.net
 * @date 2022-06-06
 */

@Slf4j
@Api(tags = "gitDown")
@CrossOrigin
@RestController
@RequestMapping("/gitDown")
public class GitDownController {

    @Autowired
    private GitDownRepository gitDownRepository;

    /**
    * 新增或编辑
    */
    @PostMapping("/save")
    @ApiOperation(value = "save GitDown", notes = "save GitDown")
    public Object save(@RequestBody GitDown gitDown){

        try {
            gitDown.down();
            return ReturnT.success(gitDownRepository.save(gitDown));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.error("保存失败");
        }

    }

    /**
    * 删除
    */
    @PostMapping("/delete")
    @ApiOperation(value = "delete GitDown", notes = "delete GitDown")
    public Object delete(int id){
        Optional<GitDown> gitDown=gitDownRepository.findById(id);
        if(gitDown.isPresent()){
            gitDownRepository.deleteById(id);
            return ReturnT.success("删除成功");
        }else{
            return ReturnT.error("没有找到该对象");
        }
    }

    /**
    * 查询
    */
    @PostMapping("/find")
    @ApiOperation(value = "find GitDown by id", notes = "find GitDown by id")
    public Object find(int id){
        Optional<GitDown> gitDown=gitDownRepository.findById(id);
        if(gitDown.isPresent()){
            return ReturnT.success(gitDown.get());
        }else{
            return ReturnT.error("没有找到该对象");
        }
    }

    /**
    * 分页查询
    */
    @PostMapping("/list")
    @ApiOperation(value = "list GitDown", notes = "list GitDown")
    public Object list(@RequestBody GitDown gitDown,
                        @RequestParam(required = false, defaultValue = "0") int pageNumber,
                        @RequestParam(required = false, defaultValue = "10") int pageSize) {

        log.info("gitDown {}",gitDown);
//        log.info();
            try {
                //创建匹配器，需要查询条件请修改此处代码
                ExampleMatcher matcher = ExampleMatcher.matchingAll();

                //创建实例
                Example<GitDown> example = Example.of(gitDown, matcher);
                //分页构造
                Pageable pageable = PageRequest.of(pageNumber,pageSize);
//                Page<GitDown> all = gitDownRepository.findAll(example, pageable);
                return ReturnT.success(gitDownRepository.findAll(example, pageable));

            } catch (Exception e) {
                e.printStackTrace();
                return ReturnT.error(e.getMessage());
            }

    }

}
